package com.nf.health.app.utils;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.view.View;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;

import java.util.ArrayList;

/**
 * Created on 2017/3/22.
 * Dialog 统一管理
 *
 * @auther changpeng
 * @email obj_cp@126.com
 */

public class MaterialDialogManager {

    private MaterialDialog mProgressDialog;
    private MaterialDialog mPositiveDialog;
    private ArrayList<MaterialDialog> materialDialogs = new ArrayList<>();
    private Context mContext;
    private Handler mHandler = new Handler();
    private MaterialDialogManager(Context context) {
        this.mContext = context;
    }

    public static MaterialDialogManager getInstance(Context context) {
        return new MaterialDialogManager(context);
    }

    public interface ListDialogItemClickListener {
        void onListItemClick(MaterialDialog dialog, View itemView, int position, CharSequence text);
    }

    public interface PositiveDialogClickListener {
        void onPositiveClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which);
        void onNegativeClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which);
    }

    public void showProgressDialog(final String title,final String content, final boolean horizontal) {
        hideAllDialog();
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mProgressDialog = new MaterialDialog.Builder(mContext)
                        .title(title)
                        .content(content)
                        .progress(true, 0)
                        .progressIndeterminateStyle(horizontal)
                        .show();
                mProgressDialog.setCanceledOnTouchOutside(false);
                materialDialogs.add(mProgressDialog);
            }
        });
    }

    public void showPositiveDialog(final String title,final String content,final String positiveText,final String negativeText,final PositiveDialogClickListener listener) {
        hideAllDialog();
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mPositiveDialog = new MaterialDialog.Builder(mContext)
                        .title(title)
                        .content(content)
                        .positiveText(positiveText)
                        .negativeText(negativeText)
                        .onNegative(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                listener.onNegativeClick(dialog,which);
                            }
                        })
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                listener.onPositiveClick(dialog,which);
                            }
                        })
                        .show();
                materialDialogs.add(mPositiveDialog);
                mPositiveDialog.setCanceledOnTouchOutside(false);
            }
        });
    }

    public void hideAllDialog(){
        for (MaterialDialog materialDialog : materialDialogs) {
            if(materialDialog != null) {
                materialDialog.dismiss();
                materialDialog = null;
            }
        }
    }

    public void hideProgressDialog(){
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mProgressDialog.dismiss();
            }
        });
    }


    public void hidePositiveDialog(){
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mPositiveDialog.dismiss();
            }
        });
    }
}
